package com.yulongtian.weekCompetition.month202212.week2;

/**
 * 第 323 场周赛
 * 可
 *
 * @author yulongTian
 * @create 2022-12-11 10:34
 */
public class Test11 {
    public static void main(String[] args) {
        int[][] grid = new int[][]{{1, 2, 4}, {3, 3, 1}};
        System.out.println(deleteGreatestValue(grid));
    }

    public static int deleteGreatestValue(int[][] grid) {
        int res = 0;
        //是否都删除 元素是否都为-1
        boolean flag = false;

        while (!flag) {
            flag = true;
            int removeMax = Integer.MIN_VALUE;//保存删除元素中的最大值
            for (int i = 0; i < grid.length; i++) {
                //一行的最大元素
                int hangMax = grid[i][0];
                int indexMax = 0;//一行最大元素索引
                for (int j = 0; j < grid[i].length; j++) {
                    if (grid[i][j] != -1) {
                        flag = false;
                        if (hangMax < grid[i][j]) {
                            hangMax = grid[i][j];
                            indexMax = j;
                        }
                    }
                }
                //改行最大元素赋值-1
                grid[i][indexMax] = -1;
                if (hangMax != -1) {
                    removeMax = Math.max(removeMax, hangMax);
                }
            }
            if (removeMax != Integer.MIN_VALUE) {
                res += removeMax;
            }
        }


        return res;
    }

}
